package com.eims.sls_app.ui.adapter;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.ImageManager;
import com.eims.sls_app.view.MyInfoListVo;
import com.eims.sls_app.view.XCRoundImageView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import bls.juejin.cn.R;

public class MyInfoListAdapter extends BaseAdapter {
	private Context context;
	private List<MyInfoListVo> infoListVos;

	public MyInfoListAdapter(Context context, List<MyInfoListVo> infoListVos) {
		super();
		this.context = context;
		this.infoListVos = infoListVos;
	}

	@Override
	public int getCount() {
		if (infoListVos == null) {
			return 0;
		}
		return infoListVos.size();

	}
	
	

	public void setInfoListVos(List<MyInfoListVo> infoListVos) {
		this.infoListVos = infoListVos;
	}

	@Override
	public Object getItem(int position) {

		return position;
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		ViewHolder holder;
		// 判断View是否为空
		if (view == null) {
			holder = new ViewHolder();

			view = LayoutInflater.from(context).inflate(R.layout.adapter_myinfo_list_item, null);

			holder.iv_Head = (XCRoundImageView) view.findViewById(R.id.iv_item_head);
			holder.tv_name = (TextView) view.findViewById(R.id.nameTv);
			holder.tv_detail = (TextView) view.findViewById(R.id.ll_tv_data);
			holder.tv_time = (TextView) view.findViewById(R.id.time);

			holder.tvContext = (TextView) view.findViewById(R.id.info);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		
		
		final String name = infoListVos.get(position).getName();
		// String name = topicBeans.get(position).name;
		final String isname;
		if (isMobileNO(name)) {
			isname = name.substring(0, 3) + "****" + name.substring(7, name.length());
			
		} else {
			isname = name;
		
		}
	
		holder.tv_name.setText(isname);
		holder.tvContext.setText(infoListVos.get(position).getContext());
		holder.tv_time.setText(infoListVos.get(position).getDatetime());
		if (!infoListVos.get(position).getDetail().equals("")&&infoListVos.get(position).getDetail()!=null) {
			holder.tv_detail.setText(infoListVos.get(position).getDetail());
		}else {
			holder.tv_detail.setText("方寸之间见世界！");
		}
		
		
		ImageManager.getInstance().displayImage(App.url + infoListVos.get(position).getHeadurl(), holder.iv_Head,
				ImageManager.getViewsHeadOptions());
		return view;
	}

	public boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("1[0-9]{10}");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	static class ViewHolder {
		XCRoundImageView iv_Head;
		TextView tv_name;
		TextView tv_detail;
		TextView tv_time;

		TextView tvContext;
	}

	public String formateRate(String rateStr) {
		if (rateStr.indexOf(".") != -1) {

			int num = 0;
			num = rateStr.indexOf(".");

			String dianAfter = rateStr.substring(0, num + 1);
			String afterData = rateStr.replace(dianAfter, "");
			if (afterData.length() < 2) {
				afterData = afterData + "0";
			} else {
				afterData = afterData;
			}
			return rateStr.substring(0, num) + "." + afterData.substring(0, 2);
		} else {
			if (rateStr == "1") {
				return "100";
			} else {
				return rateStr;
			}
		}
	}

}
